package ge.telasi.tasks.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 * User group.
 *
 * @author dimitri
 */
@Entity
@Table(name = "groups")
@NamedQueries({
    @NamedQuery(name = "Group.all", query = "SELECT g FROM Group g ORDER BY g.name")
})
public class Group extends Modifiable implements Serializable {
    private static final long serialVersionUID = 3601451408968540639L;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "GROUPS_SEQ")
	@SequenceGenerator(name = "GROUPS_SEQ", sequenceName = "GROUPS_SEQ", allocationSize = 1)
    private Integer id;
    @Column(name = "name")
    private String name;
    @Column(name = "description")
    private String description;
    @ManyToMany
    @JoinTable(name="group_users",
        joinColumns=@JoinColumn(name = "group_id", referencedColumnName="id"),
        inverseJoinColumns=@JoinColumn(name = "user_id", referencedColumnName="id"))
    private List<User> users = new ArrayList<User>();

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (id == null) {
            return false;
        }
        if (obj instanceof Group) {
            Group group = (Group) obj;
            return id.equals(group.getId());
        }
        return false;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }

}
